home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / stack.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  32KB  |  1,223 lines

  1. /* Print and select stack frames for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21.  
  22. #include "defs.h"
  23. #include "param.h"
  24. #include "language.h"
  25. #include "symtab.h"
  26. #include "frame.h"
  27. #include "gdbcmd.h"
  28. #include "value.h"
  29. #include "gdbcore.h"
  30. #include "target.h"
  31. #include "breakpoint.h"
  32.  
  33. extern int addressprint;    /* Print addresses, or stay symbolic only? */
  34. extern int info_verbose;    /* Verbosity of symbol reading msgs */
  35. extern int lines_to_list;    /* # of lines "list" command shows by default */
  36. extern char *reg_names[];    /* Names of registers */
  37.  
  38. /* Thie "selected" stack frame is used by default for local and arg access.
  39.    May be zero, for no selected frame.  */
  40.  
  41. FRAME selected_frame;
  42.  
  43. /* Level of the selected frame:
  44.    0 for innermost, 1 for its caller, ...
  45.    or -1 for frame specified by address with no defined level.  */
  46.  
  47. int selected_frame_level;
  48.  
  49. /* Nonzero means print the full filename and linenumber
  50.    when a frame is printed, and do so in a format programs can parse.  */
  51.  
  52. int frame_file_full_name = 0;
  53.  
  54. int print_frame_info ();
  55.  
  56. /* Print a stack frame briefly.  FRAME should be the frame id
  57.    and LEVEL should be its level in the stack (or -1 for level not defined).
  58.    This prints the level, the function executing, the arguments,
  59.    and the file name and line number.
  60.    If the pc is not at the beginning of the source line,
  61.    the actual pc is printed at the beginning.
  62.  
  63.    If SOURCE is 1, print the source line as well.
  64.    If SOURCE is -1, print ONLY the source line.  */
  65.  
  66. print_stack_frame (frame, level, source)
  67.      FRAME frame;
  68.      int level;
  69.      int source;
  70. {
  71.   struct frame_info *fi;
  72.  
  73.   fi = get_frame_info (frame);
  74.  
  75.   return(print_frame_info (fi, level, source, 1));
  76. }
  77.  
  78. print_frame_info (fi, level, source, args)
  79.      struct frame_info *fi;
  80.      register int level;
  81.      int source;
  82.      int args;
  83. {
  84.   struct symtab_and_line sal;
  85.   struct symbol *func;
  86.   register char *funname = 0;
  87.   int numargs;
  88.   int file;
  89.   int hide = 0;
  90.  
  91. #if 0                /* Symbol reading is fast enough now */
  92.   struct partial_symtab *pst;
  93.  
  94.   /* Don't give very much information if we haven't readin the
  95.      symbol table yet.  */
  96.   pst = find_pc_psymtab (fi->pc);
  97.   if (pst && !pst->readin)
  98.     {
  99.       /* Abbreviated information.  */
  100.       char *fname;
  101.  
  102.       if (!find_pc_partial_function (fi->pc, &fname, 0))
  103.     fname = "??";
  104.     
  105.       printf_filtered ("#%-2d ", level);
  106.       if (addressprint)
  107.         printf_filtered ("%s in ", local_hex_string(fi->pc));
  108.  
  109.       fputs_demangled (fname, stdout, -1);
  110.       fputs_filtered (" (...)\n", stdout);
  111.       
  112.       return;
  113.     }
  114. #endif
  115.  
  116.   sal = find_pc_line (fi->pc, fi->next_frame);
  117.   func = find_pc_function (fi->pc);
  118.   if (func)
  119.     {
  120.       /* In certain pathological cases, the symtabs give the wrong
  121.      function (when we are in the first function in a file which
  122.      is compiled without debugging symbols, the previous function
  123.      is compiled with debugging symbols, and the "foo.o" symbol
  124.      that is supposed to tell us where the file with debugging symbols
  125.      ends has been truncated by ar because it is longer than 15
  126.      characters).
  127.      
  128.      So look in the misc_function_vector as well, and if it comes
  129.      up with a larger address for the function use that instead.
  130.      I don't think this can ever cause any problems;
  131.      there shouldn't be any
  132.      misc_function_vector symbols in the middle of a function.  */
  133.       int misc_index = find_pc_misc_function (fi->pc);
  134.       if (misc_index >= 0
  135.       && (misc_function_vector[misc_index].address
  136.           > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
  137.     {
  138.       /* In this case we have no way of knowing the source file
  139.          and line number, so don't print them.  */
  140.       sal.symtab = 0;
  141.       /* We also don't know anything about the function besides
  142.          its address and name.  */
  143.       func = 0;
  144.       funname = misc_function_vector[misc_index].name;
  145.     }
  146.       else
  147.     funname = SYMBOL_NAME (func);
  148.     }
  149.   else
  150.     {
  151.       register int misc_index = find_pc_misc_function (fi->pc);
  152.       if (misc_index >= 0)
  153.     funname = misc_function_vector[misc_index].name;
  154.     }
  155.  
  156.   if (source >= 0 || !sal.symtab) {
  157.     
  158.     hide = hidden_lisp_frame_p(funname);
  159.  
  160.     if (hide == 0) {
  161.       if (level >= 0) printf_filtered ("#%-2d ", level);
  162.       if (addressprint)
  163.     if (fi->pc != sal.pc || !sal.symtab)
  164.       printf_filtered ("%s in ", local_hex_string(fi->pc));
  165.  
  166.       {
  167.     /* WCL stuff */
  168.     FRAME old_frame;
  169.     int old_level;
  170.     int i;
  171.     file = 1;
  172.     args = 0;
  173.  
  174.     i = find_special_frame_entry(funname);    
  175.     if (i > 0) {
  176.       source = 0;
  177.       file = 0;
  178.       old_frame = selected_frame;
  179.       old_level = selected_frame_level;
  180.       select_frame(fi,level);
  181.       print_special_lisp_frame(i);
  182.       select_frame(old_frame,old_level);
  183.     } else {
  184.       if (lisp_name_p(funname)) {
  185.         print_lisp_name(funname);
  186.       } else {
  187.         fputs_demangled (funname ? funname : "??", stdout, -1);
  188.       }
  189.     }
  190.       }
  191.  
  192.       if (args)
  193.     {
  194.       wrap_here ("   ");
  195.       fputs_filtered (" (", stdout);
  196.       FRAME_NUM_ARGS (numargs, fi);
  197.       print_frame_args (func, fi, numargs, stdout);
  198.       printf_filtered (")");
  199.     }
  200.  
  201.       if (sal.symtab && sal.symtab->filename && file)
  202.     { 
  203.       wrap_here ("   ");
  204.       printf_filtered (" at %s:%d", sal.symtab->filename, sal.line);
  205.     }
  206.       printf_filtered ("\n");
  207.     }
  208.   }
  209.  
  210.   if ((hide == 0) && (source != 0) && sal.symtab)  {
  211.     int done = 0;
  212.     int mid_statement = source < 0 && fi->pc != sal.pc;
  213.     if (frame_file_full_name)
  214.       done = identify_source_line (sal.symtab, sal.line, mid_statement);
  215.     if (!done)
  216.       {
  217.     if (addressprint && mid_statement)
  218.       printf_filtered ("%s\t", local_hex_string(fi->pc));
  219.     print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
  220.       }
  221.     current_source_line = max (sal.line - lines_to_list/2, 1);
  222.   }
  223.   if (source != 0)
  224.     set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
  225.  
  226.   fflush (stdout);
  227.   return(hide);
  228. }
  229.  
  230. void flush_cached_frames ();
  231.  
  232. #ifdef FRAME_SPECIFICATION_DYADIC
  233. extern FRAME setup_arbitrary_frame ();
  234. #endif
  235.  
  236. /*
  237.  * Read a frame specification in whatever the appropriate format is.
  238.  * Call error() if the specification is in any way invalid (i.e.
  239.  * this function never returns NULL).
  240.  */
  241. FRAME
  242. parse_frame_specification (frame_exp)
  243.      char *frame_exp;
  244. {
  245.   int numargs = 0;
  246.   int arg1, arg2;
  247.   
  248.   if (frame_exp)
  249.     {
  250.       char *addr_string, *p;
  251.       struct cleanup *tmp_cleanup;
  252.  
  253.       while (*frame_exp == ' ') frame_exp++;
  254.       for (p = frame_exp; *p && *p != ' '; p++)
  255.     ;
  256.  
  257.       if (*frame_exp)
  258.     {
  259.       numargs = 1;
  260.       addr_string = savestring(frame_exp, p - frame_exp);
  261.  
  262.       {
  263.         tmp_cleanup = make_cleanup (free, addr_string);
  264.         arg1 = parse_and_eval_address (addr_string);
  265.         do_cleanups (tmp_cleanup);
  266.       }
  267.  
  268.       while (*p == ' ') p++;
  269.       
  270.       if (*p)
  271.         {
  272.           numargs = 2;
  273.           arg2 = parse_and_eval_address (p);
  274.         }
  275.     }
  276.     }
  277.  
  278.   switch (numargs)
  279.     {
  280.     case 0:
  281.       if (selected_frame == NULL)
  282.     error ("No selected frame.");
  283.       return selected_frame;
  284.       /* NOTREACHED */
  285.     case 1:
  286.       {
  287.     int level = arg1;
  288.     FRAME fid = find_relative_frame (get_current_frame (), &level);
  289.     FRAME tfid;
  290.  
  291.     if (level == 0)
  292.       /* find_relative_frame was successful */
  293.       return fid;
  294.  
  295.     /* If (s)he specifies the frame with an address, he deserves what
  296.        (s)he gets.  Still, give the highest one that matches.  */
  297.  
  298.     for (fid = get_current_frame ();
  299.          fid && FRAME_FP (fid) != arg1;
  300.          fid = get_prev_frame (fid))
  301.       ;
  302.  
  303.     if (fid)
  304.       while ((tfid = get_prev_frame (fid)) &&
  305.          (FRAME_FP (tfid) == arg1))
  306.         fid = tfid;
  307.       
  308. #ifdef FRAME_SPECIFICATION_DYADIC
  309.     if (!fid)
  310.       error ("Incorrect number of args in frame specification");
  311.  
  312.     return fid;
  313. #else
  314.     return create_new_frame (arg1, 0);
  315. #endif
  316.       }
  317.       /* NOTREACHED */
  318.     case 2:
  319.       /* Must be addresses */
  320. #ifndef FRAME_SPECIFICATION_DYADIC
  321.       error ("Incorrect number of args in frame specification");
  322. #else
  323.       return setup_arbitrary_frame (arg1, arg2);
  324. #endif
  325.       /* NOTREACHED */
  326.     }
  327.   fatal ("Internal: Error in parsing in parse_frame_specification");
  328.   /* NOTREACHED */
  329. }
  330.  
  331. /* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
  332.    that if it is unsure about the answer, it returns 0
  333.    instead of guessing (this happens on the VAX and i960, for example).
  334.  
  335.    On most machines, we never have to guess about the args address,
  336.    so FRAME_ARGS_ADDRESS{,_CORRECT} are the same.  */
  337. #if !defined (FRAME_ARGS_ADDRESS_CORRECT)
  338. #define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
  339. #endif
  340.  
  341. /* Print verbosely the selected frame or the frame at address ADDR.
  342.    This means absolutely all information in the frame is printed.  */
  343.  
  344. static void
  345. frame_info (addr_exp)
  346.      char *addr_exp;
  347. {
  348.   FRAME frame;
  349.   struct frame_info *fi;
  350.   struct frame_saved_regs fsr;
  351.   struct symtab_and_line sal;
  352.   struct symbol *func;
  353.   struct symtab *s;
  354.   FRAME calling_frame;
  355.   int i, count;
  356.   char *funname = 0;
  357.  
  358.   if (!target_has_stack)
  359.     error ("No inferior or core file.");
  360.  
  361.   frame = parse_frame_specification (addr_exp);
  362.   if (!frame)
  363.     error ("Invalid frame specified.");
  364.  
  365.   fi = get_frame_info (frame);
  366.   sal = find_pc_line (fi->pc, fi->next_frame);
  367.   func = get_frame_function (frame);
  368.   s = find_pc_symtab(fi->pc);
  369.   if (func)
  370.     funname = SYMBOL_NAME (func);
  371.   else
  372.     {
  373.       register int misc_index = find_pc_misc_function (fi->pc);
  374.       if (misc_index >= 0)
  375.     funname = misc_function_vector[misc_index].name;
  376.     }
  377.   calling_frame = get_prev_frame (frame);
  378.  
  379.   if (!addr_exp && selected_frame_level >= 0) {
  380.     printf_filtered ("Stack level %d, frame at %s:\n",
  381.              selected_frame_level, 
  382.              local_hex_string(FRAME_FP(frame)));
  383.   } else {
  384.     printf_filtered ("Stack frame at %s:\n",
  385.              local_hex_string(FRAME_FP(frame)));
  386.   }
  387.   printf_filtered (" %s = %s",
  388.            reg_names[PC_REGNUM], 
  389.            local_hex_string(fi->pc));
  390.  
  391.   wrap_here ("   ");
  392.   if (funname)
  393.     {
  394.       printf_filtered (" in ");
  395.       fputs_demangled (funname, stdout, 1);
  396.     }
  397.   wrap_here ("   ");
  398.   if (sal.symtab)
  399.     printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
  400.   puts_filtered ("; ");
  401.   wrap_here ("    ");
  402.   printf_filtered ("saved %s %s\n", reg_names[PC_REGNUM],
  403.            local_hex_string(FRAME_SAVED_PC (frame)));
  404.   if (calling_frame)
  405.     printf_filtered (" called by frame at %s", 
  406.              local_hex_string(FRAME_FP (calling_frame)));
  407.   if (fi->next_frame && calling_frame)
  408.     puts_filtered (",");
  409.   wrap_here ("   ");
  410.   if (fi->next_frame)
  411.     printf_filtered (" caller of frame at %s", local_hex_string(fi->next_frame));
  412.   if (fi->next_frame || calling_frame)
  413.     puts_filtered ("\n");
  414.   if (s)
  415.      printf_filtered(" source language %s.\n", language_str(s->language));
  416.  
  417.   {
  418.     /* Address of the argument list for this frame, or 0.  */
  419.     CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
  420.     /* Number of args for this frame, or -1 if unknown.  */
  421.     int numargs;
  422.  
  423.     if (arg_list == 0)
  424.     printf_filtered (" Arglist at unknown address.\n");
  425.     else
  426.       {
  427.     printf_filtered (" Arglist at %s,", local_hex_string(arg_list));
  428.  
  429.     FRAME_NUM_ARGS (numargs, fi);
  430.     if (numargs < 0)
  431.       puts_filtered (" args: ");
  432.     else if (numargs == 0)
  433.       puts_filtered (" no args.");
  434.     else if (numargs == 1)
  435.       puts_filtered (" 1 arg: ");
  436.     else
  437.       printf_filtered (" %d args: ", numargs);
  438.     print_frame_args (func, fi, numargs, stdout);
  439.     puts_filtered ("\n");
  440.       }
  441.   }
  442.  
  443. #if defined (FRAME_FIND_SAVED_REGS)  
  444.   get_frame_saved_regs (fi, &fsr);
  445.   /* The sp is special; what's returned isn't the save address, but
  446.      actually the value of the previous frame's sp.  */
  447.   printf_filtered (" Previous frame's sp is %s\n", 
  448.            local_hex_string(fsr.regs[SP_REGNUM]));
  449.   count = 0;
  450.   for (i = 0; i < NUM_REGS; i++)
  451.     if (fsr.regs[i] && i != SP_REGNUM)
  452.       {
  453.     if (count == 0)
  454.       puts_filtered (" Saved registers:\n ");
  455.     else
  456.       puts_filtered (",");
  457.     wrap_here (" ");
  458.     printf_filtered (" %s at %s", reg_names[i], 
  459.              local_hex_string(fsr.regs[i]));
  460.     count++;
  461.       }
  462.   if (count)
  463.     puts_filtered ("\n");
  464. #endif /* Have FRAME_FIND_SAVED_REGS.  */
  465. }
  466.  
  467. #if 0
  468. /* Set a limit on the number of frames printed by default in a
  469.    backtrace.  */
  470.  
  471. static int backtrace_limit;
  472.  
  473. static void
  474. set_backtrace_limit_command (count_exp, from_tty)
  475.      char *count_exp;
  476.      int from_tty;
  477. {
  478.   int count = parse_and_eval_address (count_exp);
  479.  
  480.   if (count < 0)
  481.     error ("Negative argument not meaningful as backtrace limit.");
  482.  
  483.   backtrace_limit = count;
  484. }
  485.  
  486. static void
  487. backtrace_limit_info (arg, from_tty)
  488.      char *arg;
  489.      int from_tty;
  490. {
  491.   if (arg)
  492.     error ("\"Info backtrace-limit\" takes no arguments.");
  493.  
  494.   printf ("Backtrace limit: %d.\n", backtrace_limit);
  495. }
  496. #endif
  497.  
  498. /* Print briefly all stack frames or just the innermost COUNT frames.  */
  499.  
  500. static void
  501. backtrace_command (count_exp, from_tty)
  502.      char *count_exp;
  503.      int from_tty;
  504. {
  505.   struct frame_info *fi;
  506.   register int count;
  507.   register FRAME frame;
  508.   register int i;
  509.   register FRAME trailing;
  510.   register int trailing_level;
  511.  
  512.   if (!target_has_stack)
  513.     error ("No stack.");
  514.  
  515.   /* The following code must do two things.  First, it must
  516.      set the variable TRAILING to the frame from which we should start
  517.      printing.  Second, it must set the variable count to the number
  518.      of frames which we should print, or -1 if all of them.  */
  519.   trailing = get_current_frame ();
  520.   trailing_level = 0;
  521.   if (count_exp)
  522.     {
  523.       count = parse_and_eval_address (count_exp);
  524.       if (count < 0)
  525.     {
  526.       FRAME current;
  527.  
  528.       count = -count;
  529.  
  530.       current = trailing;
  531.       while (current && count--)
  532.         {
  533.           QUIT;
  534.           current = get_prev_frame (current);
  535.         }
  536.       
  537.       /* Will stop when CURRENT reaches the top of the stack.  TRAILING
  538.          will be COUNT below it.  */
  539.       while (current)
  540.         {
  541.           QUIT;
  542.           trailing = get_prev_frame (trailing);
  543.           current = get_prev_frame (current);
  544.           trailing_level++;
  545.         }
  546.       
  547.       count = -1;
  548.     }
  549.     }
  550.   else
  551.     count = -1;
  552.  
  553.   if (info_verbose)
  554.     {
  555.       struct partial_symtab *ps;
  556.       
  557.       /* Read in symbols for all of the frames.  Need to do this in
  558.      a separate pass so that "Reading in symbols for xxx" messages
  559.      don't screw up the appearance of the backtrace.  Also
  560.      if people have strong opinions against reading symbols for
  561.      backtrace this may have to be an option.  */
  562.       i = count;
  563.       for (frame = trailing;
  564.        frame != NULL && i--;
  565.        frame = get_prev_frame (frame))
  566.     {
  567.       QUIT;
  568.       fi = get_frame_info (frame);
  569.       ps = find_pc_psymtab (fi->pc);
  570.       if (ps)
  571.         (void) PSYMTAB_TO_SYMTAB (ps);    /* Force syms to come in */
  572.     }
  573.     }
  574.  
  575.   for (i = 0, frame = trailing;
  576.        frame && count--;
  577.        i++, frame = get_prev_frame (frame))
  578.     {
  579.       QUIT;
  580.       fi = get_frame_info (frame);
  581.       print_frame_info (fi, trailing_level + i, 0, 1);
  582.     }
  583.  
  584.   /* If we've stopped before the end, mention that.  */
  585.   if (frame && from_tty)
  586.     printf_filtered ("(More stack frames follow...)\n");
  587. }
  588.  
  589. /* Print the local variables of a block B active in FRAME.
  590.    Return 1 if any variables were printed; 0 otherwise.  */
  591.  
  592. static int
  593. print_block_frame_locals (b, frame, stream)
  594.      struct block *b;
  595.      register FRAME frame;
  596.      register FILE *stream;
  597. {
  598.   int nsyms;
  599.   register int i;
  600.   register struct symbol *sym;
  601.   register int values_printed = 0;
  602.  
  603.   nsyms = BLOCK_NSYMS (b);
  604.  
  605.   for (i = 0; i < nsyms; i++)
  606.     {
  607.       sym = BLOCK_SYM (b, i);
  608.       if (SYMBOL_CLASS (sym) == LOC_LOCAL
  609.       || SYMBOL_CLASS (sym) == LOC_REGISTER
  610.       || SYMBOL_CLASS (sym) == LOC_STATIC)
  611.     {
  612.       values_printed = 1;
  613.       fprint_symbol (stream, SYMBOL_NAME (sym));
  614.       fputs_filtered (" = ", stream);
  615.       print_variable_value (sym, frame, stream);
  616.       fprintf_filtered (stream, "\n");
  617.       fflush (stream);
  618.     }
  619.     }
  620.   return values_printed;
  621. }
  622.  
  623. /* Same, but print labels.  */
  624.  
  625. static int
  626. print_block_frame_labels (b, have_default, stream)
  627.      struct block *b;
  628.      int *have_default;
  629.      register FILE *stream;
  630. {
  631.   int nsyms;
  632.   register int i;
  633.   register struct symbol *sym;
  634.   register int values_printed = 0;
  635.  
  636.   nsyms = BLOCK_NSYMS (b);
  637.  
  638.   for (i = 0; i < nsyms; i++)
  639.     {
  640.       sym = BLOCK_SYM (b, i);
  641.       if (! strcmp (SYMBOL_NAME (sym), "default"))
  642.     {
  643.       if (*have_default)
  644.         continue;
  645.       *have_default = 1;
  646.     }
  647.       if (SYMBOL_CLASS (sym) == LOC_LABEL)
  648.     {
  649.       struct symtab_and_line sal;
  650.       sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
  651.       values_printed = 1;
  652.       fputs_demangled (SYMBOL_NAME (sym), stream, 1);
  653.       if (addressprint)
  654.         fprintf_filtered (stream, " %s", 
  655.                   local_hex_string(SYMBOL_VALUE_ADDRESS (sym)));
  656.       fprintf_filtered (stream, " in file %s, line %d\n",
  657.                 sal.symtab->filename, sal.line);
  658.       fflush (stream);
  659.     }
  660.     }
  661.   return values_printed;
  662. }
  663.  
  664. /* Print on STREAM all the local variables in frame FRAME,
  665.    including all the blocks active in that frame
  666.    at its current pc.
  667.  
  668.    Returns 1 if the job was done,
  669.    or 0 if nothing was printed because we have no info
  670.    on the function running in FRAME.  */
  671.  
  672. static int
  673. print_frame_local_vars (frame, stream)
  674.      register FRAME frame;
  675.      register FILE *stream;
  676. {
  677.   register struct block *block = get_frame_block (frame);
  678.   register int values_printed = 0;
  679.  
  680.   if (block == 0)
  681.     {
  682.       fprintf_filtered (stream, "No symbol table info available.\n");
  683.       fflush (stream);
  684.       return 0;
  685.     }
  686.   
  687.   while (block != 0)
  688.     {
  689.       if (print_block_frame_locals (block, frame, stream))
  690.     values_printed = 1;
  691.       /* After handling the function's top-level block, stop.
  692.      Don't continue to its superblock, the block of
  693.      per-file symbols.  */
  694.       if (BLOCK_FUNCTION (block))
  695.     break;
  696.       block = BLOCK_SUPERBLOCK (block);
  697.     }
  698.  
  699.   if (!values_printed)
  700.     {
  701.       fprintf_filtered (stream, "No locals.\n");
  702.       fflush (stream);
  703.     }
  704.   
  705.   return 1;
  706. }
  707.  
  708. /* Same, but print labels.  */
  709.  
  710. static int
  711. print_frame_label_vars (frame, this_level_only, stream)
  712.      register FRAME frame;
  713.      int this_level_only;
  714.      register FILE *stream;
  715. {
  716.   extern struct blockvector *blockvector_for_pc ();
  717.   register struct blockvector *bl;
  718.   register struct block *block = get_frame_block (frame);
  719.   register int values_printed = 0;
  720.   int index, have_default = 0;
  721.   char *blocks_printed;
  722.   struct frame_info *fi = get_frame_info (frame);
  723.   CORE_ADDR pc = fi->pc;
  724.  
  725.   if (block == 0)
  726.     {
  727.       fprintf_filtered (stream, "No symbol table info available.\n");
  728.       fflush (stream);
  729.       return 0;
  730.     }
  731.  
  732.   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
  733.   blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
  734.   bzero (blocks_printed, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
  735.  
  736.   while (block != 0)
  737.     {
  738.       CORE_ADDR end = BLOCK_END (block) - 4;
  739.       int last_index;
  740.  
  741.       if (bl != blockvector_for_pc (end, &index))
  742.     error ("blockvector blotch");
  743.       if (BLOCKVECTOR_BLOCK (bl, index) != block)
  744.     error ("blockvector botch");
  745.       last_index = BLOCKVECTOR_NBLOCKS (bl);
  746.       index += 1;
  747.  
  748.       /* Don't print out blocks that have gone by.  */
  749.       while (index < last_index
  750.          && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
  751.     index++;
  752.  
  753.       while (index < last_index
  754.          && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
  755.     {
  756.       if (blocks_printed[index] == 0)
  757.         {
  758.           if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
  759.         values_printed = 1;
  760.           blocks_printed[index] = 1;
  761.         }
  762.       index++;
  763.     }
  764.       if (have_default)
  765.     return 1;
  766.       if (values_printed && this_level_only)
  767.     return 1;
  768.  
  769.       /* After handling the function's top-level block, stop.
  770.      Don't continue to its superblock, the block of
  771.      per-file symbols.  */
  772.       if (BLOCK_FUNCTION (block))
  773.     break;
  774.       block = BLOCK_SUPERBLOCK (block);
  775.     }
  776.  
  777.   if (!values_printed && !this_level_only)
  778.     {
  779.       fprintf_filtered (stream, "No catches.\n");
  780.       fflush (stream);
  781.     }
  782.   
  783.   return values_printed;
  784. }
  785.  
  786. /* ARGSUSED */
  787. static void
  788. locals_info (args, from_tty)
  789.      char *args;
  790.      int from_tty;
  791. {
  792.   if (!selected_frame)
  793.     error ("No frame selected.");
  794.   print_frame_local_vars (selected_frame, stdout);
  795. }
  796.  
  797. static void
  798. catch_info ()
  799. {
  800.   if (!selected_frame)
  801.     error ("No frame selected.");
  802.   print_frame_label_vars (selected_frame, 0, stdout);
  803. }
  804.  
  805. static int
  806. print_frame_arg_vars (frame, stream)
  807.      register FRAME frame;
  808.      register FILE *stream;
  809. {
  810.   struct symbol *func = get_frame_function (frame);
  811.   register struct block *b;
  812.   int nsyms;
  813.   register int i;
  814.   register struct symbol *sym, *sym2;
  815.   register int values_printed = 0;
  816.  
  817.   if (func == 0)
  818.     {
  819.       fprintf_filtered (stream, "No symbol table info available.\n");
  820.       fflush (stream);
  821.       return 0;
  822.     }
  823.  
  824.   b = SYMBOL_BLOCK_VALUE (func);
  825.   nsyms = BLOCK_NSYMS (b);
  826.  
  827.   for (i = 0; i < nsyms; i++)
  828.     {
  829.       sym = BLOCK_SYM (b, i);
  830.       if (SYMBOL_CLASS (sym) == LOC_ARG
  831.       || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
  832.       || SYMBOL_CLASS (sym) == LOC_REF_ARG
  833.       || SYMBOL_CLASS (sym) == LOC_REGPARM)
  834.     {
  835.       values_printed = 1;
  836.       fprint_symbol (stream, SYMBOL_NAME (sym));
  837.       fputs_filtered (" = ", stream);
  838.       /* We have to look up the symbol because arguments often have
  839.          two entries (one a parameter, one a register) and the one
  840.          we want is the register, which lookup_symbol will find for
  841.          us.  */
  842.       sym2 = lookup_symbol (SYMBOL_NAME (sym),
  843.             b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
  844.       print_variable_value (sym2, frame, stream);
  845.       fprintf_filtered (stream, "\n");
  846.       fflush (stream);
  847.     }
  848.     }
  849.  
  850.   if (!values_printed)
  851.     {
  852.       fprintf_filtered (stream, "No arguments.\n");
  853.       fflush (stream);
  854.     }
  855.  
  856.   return 1;
  857. }
  858.  
  859. static void
  860. args_info ()
  861. {
  862.   if (!selected_frame)
  863.     error ("No frame selected.");
  864.   print_frame_arg_vars (selected_frame, stdout);
  865. }
  866.  
  867. /* Select frame FRAME, and note that its stack level is LEVEL.
  868.    LEVEL may be -1 if an actual level number is not known.  */
  869.  
  870. void
  871. select_frame (frame, level)
  872.      FRAME frame;
  873.      int level;
  874. {
  875.   register struct symtab *s;
  876.  
  877.   selected_frame = frame;
  878.   selected_frame_level = level;
  879.  
  880.   /* Ensure that symbols for this frame are read in.  Also, determine the
  881.      source language of this frame, and switch to it if desired.  */
  882.   if (frame)
  883.   {
  884.     s = find_pc_symtab (get_frame_info (frame)->pc);
  885.     if (s 
  886.     && s->language != current_language->la_language
  887.     && s->language != language_unknown
  888.     && language_mode == language_mode_auto) {
  889.       set_language(s->language);
  890.     }
  891.   }
  892. }
  893.  
  894. /* Store the selected frame and its level into *FRAMEP and *LEVELP.
  895.    If there is no selected frame, *FRAMEP is set to NULL.  */
  896.  
  897. void
  898. record_selected_frame (frameaddrp, levelp)
  899.      FRAME_ADDR *frameaddrp;
  900.      int *levelp;
  901. {
  902.   *frameaddrp = selected_frame ? FRAME_FP (selected_frame) : NULL;
  903.   *levelp = selected_frame_level;
  904. }
  905.  
  906. /* Return the symbol-block in which the selected frame is executing.
  907.    Can return zero under various legitimate circumstances.  */
  908.  
  909. struct block *
  910. get_selected_block ()
  911. {
  912.   if (!target_has_stack)
  913.     return 0;
  914.  
  915.   if (!selected_frame)
  916.     return get_current_block ();
  917.   return get_frame_block (selected_frame);
  918. }
  919.  
  920. /* Find a frame a certain number of levels away from FRAME.
  921.    LEVEL_OFFSET_PTR points to an int containing the number of levels.
  922.    Positive means go to earlier frames (up); negative, the reverse.
  923.    The int that contains the number of levels is counted toward
  924.    zero as the frames for those levels are found.
  925.    If the top or bottom frame is reached, that frame is returned,
  926.    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
  927.    how much farther the original request asked to go.  */
  928.  
  929. FRAME
  930. find_relative_frame (frame, level_offset_ptr)
  931.      register FRAME frame;
  932.      register int* level_offset_ptr;
  933. {
  934.   register FRAME prev;
  935.   register FRAME frame1;
  936.  
  937.   /* Going up is simple: just do get_prev_frame enough times
  938.      or until initial frame is reached.  */
  939.   while (*level_offset_ptr > 0)
  940.     {
  941.       prev = get_prev_frame (frame);
  942.       if (prev == 0)
  943.     break;
  944.       (*level_offset_ptr)--;
  945.       frame = prev;
  946.     }
  947.   /* Going down is just as simple.  */
  948.   if (*level_offset_ptr < 0)
  949.     {
  950.       while (*level_offset_ptr < 0) {
  951.     frame1 = get_next_frame (frame);
  952.     if (!frame1)
  953.       break;
  954.     frame = frame1;
  955.     (*level_offset_ptr)++;
  956.       }
  957.     }
  958.   return frame;
  959. }
  960.  
  961. /* The "frame" command.  With no arg, print selected frame briefly.
  962.    With arg LEVEL_EXP, select the frame at level LEVEL if it is a
  963.    valid level.  Otherwise, treat level_exp as an address expression
  964.    and print it.  See parse_frame_specification for more info on proper
  965.    frame expressions. */
  966.  
  967. static void
  968. frame_command (level_exp, from_tty)
  969.      char *level_exp;
  970.      int from_tty;
  971. {
  972.   register FRAME frame, frame1;
  973.   unsigned int level = 0;
  974.  
  975.   if (!target_has_stack)
  976.     error ("No stack.");
  977.  
  978.   frame = parse_frame_specification (level_exp);
  979.  
  980.   /* Try to figure out what level this frame is.  But if there is
  981.      no current stack, don't error out -- let the user set one.  */
  982.   frame1 = 0;
  983.   if (get_current_frame()) {
  984.     for (frame1 = get_prev_frame (0);
  985.      frame1 && frame1 != frame;
  986.      frame1 = get_prev_frame (frame1))
  987.       level++;
  988.   }
  989.  
  990.   if (!frame1)
  991.     level = 0;
  992.  
  993.   select_frame (frame, level);
  994.  
  995.   if (!from_tty)
  996.     return;
  997.  
  998.   print_stack_frame (selected_frame, selected_frame_level, 1);
  999. }
  1000.  
  1001. /* Select the frame up one or COUNT stack levels
  1002.    from the previously selected frame, and print it briefly.  */
  1003.  
  1004. /* ARGSUSED */
  1005. static void
  1006. up_silently_command (count_exp, from_tty)
  1007.      char *count_exp;
  1008.      int from_tty;
  1009. {
  1010.   register FRAME frame;
  1011.   int count = 1, count1;
  1012.   if (count_exp)
  1013.     count = parse_and_eval_address (count_exp);
  1014.   count1 = count;
  1015.   
  1016.   if (!target_has_stack)
  1017.     error ("No stack.");
  1018.  
  1019.   frame = find_relative_frame (selected_frame, &count1);
  1020.   if (count1 != 0 && count_exp == 0)
  1021.     error ("Initial frame selected; you cannot go up.");
  1022.   select_frame (frame, selected_frame_level + count - count1);
  1023. }
  1024.  
  1025. static void
  1026. up_command (count_exp, from_tty)
  1027.      char *count_exp;
  1028.      int from_tty;
  1029. {
  1030.   int hide = 1; 
  1031.   up_silently_command (count_exp, from_tty);
  1032.   while (hide == 1) {
  1033.     hide = print_stack_frame (selected_frame, selected_frame_level, 1);
  1034.     if (hide == 1) {
  1035.       up_silently_command("1", 0);
  1036.     }
  1037.   }
  1038. }
  1039.  
  1040. /* Select the frame down one or COUNT stack levels
  1041.    from the previously selected frame, and print it briefly.  */
  1042.  
  1043. /* ARGSUSED */
  1044. static void
  1045. down_silently_command (count_exp, from_tty)
  1046.      char *count_exp;
  1047.      int from_tty;
  1048. {
  1049.   register FRAME frame;
  1050.   int count = -1, count1;
  1051.   if (count_exp)
  1052.     count = - parse_and_eval_address (count_exp);
  1053.   count1 = count;
  1054.   
  1055.   if (!target_has_stack)
  1056.     error ("No stack.");
  1057.  
  1058.   frame = find_relative_frame (selected_frame, &count1);
  1059.   if (count1 != 0 && count_exp == 0)
  1060.     error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
  1061.   select_frame (frame, selected_frame_level + count - count1);
  1062. }
  1063.  
  1064.  
  1065. static void
  1066. down_command (count_exp, from_tty)
  1067.      char *count_exp;
  1068.      int from_tty;
  1069. {
  1070.   int hide = 1; 
  1071.   down_silently_command (count_exp, from_tty);
  1072.   while (hide == 1) {
  1073.     hide = print_stack_frame (selected_frame, selected_frame_level, 1);
  1074.     if (hide == 1) {
  1075.       down_silently_command ("1",0);
  1076.     }
  1077.   }
  1078. }
  1079.  
  1080. static void
  1081. return_command (retval_exp, from_tty)
  1082.      char *retval_exp;
  1083.      int from_tty;
  1084. {
  1085.   struct symbol *thisfun;
  1086.   FRAME_ADDR selected_frame_addr;
  1087.   CORE_ADDR selected_frame_pc;
  1088.   FRAME frame;
  1089.  
  1090.   if (selected_frame == NULL)
  1091.     error ("No selected frame.");
  1092.   thisfun = get_frame_function (selected_frame);
  1093.   selected_frame_addr = FRAME_FP (selected_frame);
  1094.   selected_frame_pc = (get_frame_info (selected_frame))->pc;
  1095.  
  1096.   /* If interactive, require confirmation.  */
  1097.  
  1098.   if (from_tty)
  1099.     {
  1100.       if (thisfun != 0)
  1101.     {
  1102.       if (!query ("Make %s return now? ", SYMBOL_NAME (thisfun)))
  1103.         error ("Not confirmed.");
  1104.     }
  1105.       else
  1106.     if (!query ("Make selected stack frame return now? "))
  1107.       error ("Not confirmed.");
  1108.     }
  1109.  
  1110.   /* Do the real work.  Pop until the specified frame is current.  We
  1111.      use this method because the selected_frame is not valid after
  1112.      a POP_FRAME.  The pc comparison makes this work even if the
  1113.      selected frame shares its fp with another frame.  */
  1114.  
  1115.   while ( selected_frame_addr != FRAME_FP (frame = get_current_frame())
  1116.        || selected_frame_pc   != (get_frame_info (frame))->pc  )
  1117.     POP_FRAME;
  1118.  
  1119.   /* Then pop that frame.  */
  1120.  
  1121.   POP_FRAME;
  1122.  
  1123.   /* Compute the return value (if any) and store in the place
  1124.      for return values.  */
  1125.  
  1126.   if (retval_exp)
  1127.     set_return_value (parse_and_eval (retval_exp));
  1128.  
  1129.   /* If interactive, print the frame that is now current.  */
  1130.  
  1131.   if (from_tty)
  1132.     frame_command ("0", 1);
  1133. }
  1134.  
  1135. /* Gets the language of the current frame. */
  1136. enum language
  1137. get_frame_language()
  1138. {
  1139.    register struct symtab *s;
  1140.    FRAME fr;
  1141.    enum language flang;        /* The language of the current frame */
  1142.    
  1143.    fr = get_frame_info(selected_frame);
  1144.    if(fr)
  1145.    {
  1146.       s = find_pc_symtab(fr->pc);
  1147.       if(s)
  1148.      flang = s->language;
  1149.       else
  1150.      flang = language_unknown;
  1151.    }
  1152.    else
  1153.       flang = language_unknown;
  1154.  
  1155.    return flang;
  1156. }
  1157.  
  1158. void
  1159. _initialize_stack ()
  1160. {
  1161. #if 0  
  1162.   backtrace_limit = 30;
  1163. #endif
  1164.  
  1165.   add_com ("return", class_stack, return_command,
  1166.        "Make selected stack frame return to its caller.\n\
  1167. Control remains in the debugger, but when you continue\n\
  1168. execution will resume in the frame above the one now selected.\n\
  1169. If an argument is given, it is an expression for the value to return.");
  1170.  
  1171.   add_com ("up", class_stack, up_command,
  1172.        "Select and print stack frame that called this one.\n\
  1173. An argument says how many frames up to go.");
  1174.   add_com ("up-silently", class_support, up_silently_command,
  1175.        "Same as the `up' command, but does not print anything.\n\
  1176. This is useful in command scripts.");
  1177.  
  1178.   add_com ("down", class_stack, down_command,
  1179.        "Select and print stack frame called by this one.\n\
  1180. An argument says how many frames down to go.");
  1181.   add_com_alias ("do", "down", class_stack, 1);
  1182.   add_com ("down-silently", class_support, down_silently_command,
  1183.        "Same as the `down' command, but does not print anything.\n\
  1184. This is useful in command scripts.");
  1185.  
  1186.   add_com ("frame", class_stack, frame_command,
  1187.        "Select and print a stack frame.\n\
  1188. With no argument, print the selected stack frame.  (See also \"info frame\").\n\
  1189. An argument specifies the frame to select.\n\
  1190. It can be a stack frame number or the address of the frame.\n\
  1191. With argument, nothing is printed if input is coming from\n\
  1192. a command file or a user-defined command.");
  1193.  
  1194.   add_com_alias ("f", "frame", class_stack, 1);
  1195.  
  1196.   add_com ("backtrace", class_stack, backtrace_command,
  1197.        "Print backtrace of all stack frames, or innermost COUNT frames.\n\
  1198. With a negative argument, print outermost -COUNT frames.");
  1199.   add_com_alias ("bt", "backtrace", class_stack, 0);
  1200.   add_com_alias ("where", "backtrace", class_alias, 0);
  1201.   add_info ("stack", backtrace_command,
  1202.         "Backtrace of the stack, or innermost COUNT frames.");
  1203.   add_info_alias ("s", "stack", 1);
  1204.   add_info ("frame", frame_info,
  1205.         "All about selected stack frame, or frame at ADDR.");
  1206.   add_info_alias ("f", "frame", 1);
  1207.   add_info ("locals", locals_info,
  1208.         "Local variables of current stack frame.");
  1209.   add_info ("args", args_info,
  1210.         "Argument variables of current stack frame.");
  1211.   add_info ("catch", catch_info,
  1212.         "Exceptions that can be caught in the current stack frame.");
  1213.  
  1214. #if 0
  1215.   add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, 
  1216.        "Specify maximum number of frames for \"backtrace\" to print by default.",
  1217.        &setlist);
  1218.   add_info ("backtrace-limit", backtrace_limit_info,
  1219.         "The maximum number of frames for \"backtrace\" to print by default.");
  1220. #endif
  1221. }
  1222.  
  1223.